﻿using Comm;
using Kinlo.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Repo
{
    public class CodeFirstRep
    {
        string strConn;
        public CodeFirstRep()
        {
            strConn = System.Configuration.ConfigurationManager.ConnectionStrings["WeightDB"].ConnectionString;

        }
        public int DeleteCodeFirst(CodeFirst w)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {

                    var cmd = string.Format(@"DELETE dbo.Code_First WHERE Code='{0}'", w.Code);
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "DeleteCodeFirst", ex);
                return 0;
            }
        }

        public int Add2(CodeFirst w)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {
                    var cmd = @"INSERT dbo.Code_First  ( CodeIndex, Code, ProductDate ,IsValidate)
                        VALUES  ( @CodeIndex, @Code, @ProductDate ,@IsValidate)";

                    var sqlCmd = new SqlCommand(cmd, conn);
                    sqlCmd.Parameters.Add(new SqlParameter("@Code", w.Code == null ? DBNull.Value : (object)w.Code));
                    sqlCmd.Parameters.Add(new SqlParameter("@CodeIndex", w.CodeIndex == null ? DBNull.Value : (object)w.CodeIndex));
                    sqlCmd.Parameters.Add(new SqlParameter("@ProductDate", w.ProductDate == null ? DBNull.Value : (object)w.ProductDate));
                    sqlCmd.Parameters.Add(new SqlParameter("@IsValidate", w.IsValidate == null ? DBNull.Value : (object)w.IsValidate));
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "Add2", ex);
                return 0;
            }
        }

        public int Add(CodeFirst w)
        {
            try
            {
                //var cmd = @"INSERT dbo.Code_First  ( CodeIndex, Code, ProductDate ,IsValidate)
                //        VALUES  ( @CodeIndex, @Code, @ProductDate ,@IsValidate)";

                //var result = DB.ExecuteSqlCommandAsync(TransactionalBehavior.DoNotEnsureTransaction, cmd,
                //         new SqlParameter("@Code", w.Code == null ? DBNull.Value : (object)w.Code),
                //         new SqlParameter("@CodeIndex", w.CodeIndex == null ? DBNull.Value : (object)w.CodeIndex),
                //         new SqlParameter("@ProductDate", w.ProductDate == null ? DBNull.Value : (object)w.ProductDate),
                //         new SqlParameter("@IsValidate", w.IsValidate == null ? DBNull.Value : (object)w.IsValidate)
                //     );
                //return result.Result;
                return 0;

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "Add", ex);
                return 0;
            }

        }

        /// <summary>
        /// 更新状态 是否已失效
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int Update(string code)
        {
            try
            {
                //var cmd = @"UPDATE Code_First
                //        SET IsValidate=0
                //        WHERE Code=@Code";

                //return DB.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, cmd,
                //         new SqlParameter("@Code", code == null ? DBNull.Value : (object)code));
                return 0;

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "Update", ex);
                return 0;
            }
        }

        public CodeFirst GetCodeFirst(string code)
        {
            try
            {
                //var cmd = "SELECT * FROM dbo.Code_First WHERE Code=@Code";
                //return DB.SqlQuery<CodeFirst>(cmd,
                //    new SqlParameter("@Code", code)).FirstOrDefault();
                return null;

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "Update", ex);
                return null;
            }

        }
        public IList<CodeFirst> GetCodeFirst1()
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {
                    // var cmd = @"SELECT * FROM dbo.Code_First WHERE IsValidate=1 ORDER BY ProductDate DESC";
                    var cmd = @"SELECT TOP 1000 * FROM dbo.Code_First WHERE IsValidate=1 AND	ProductDate>DATEADD(DAY,-7,GETDATE()) ORDER BY ProductDate DESC";
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    var CodeFirst = sqlCmd.ExecuteReader().ReaderToList<CodeFirst>();
                    if (CodeFirst.Count > 0)
                        return CodeFirst;
                }
                return null;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "Update", ex);
                return null;
            }
        }


        public List<CodeFirst> GetCodeFirst()
        {
            //var cmd = @"SELECT * FROM dbo.Code_First WHERE IsValidate=1 ORDER BY ProductDate DESC";
            //return DB.SqlQuery<CodeFirst>(cmd).ToList();
            return null;
        }

        /// <summary>
        /// 请除 day 天前的记录
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        public int ClearRecordByDay(int day = 0)
        {
            try
            {
                //var cmd = @"EXEC dbo.ClearRecordByDay @day = 0";
                //return DB.ExecuteSqlCommand(cmd,
                //    new SqlParameter("@day", day));
                return 0;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "CodeFirstRep", "ClearRecordByDay", ex);
                return 0;
            }

        }

    }

}
